package com.company.common.function.webservice.NoCxfWsdl;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.util.HttpURLConnection;
import org.apache.cxf.helpers.IOUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.io.*;
import java.net.URL;

public class wsdlSend {
    public static void main(String[] args) throws Exception {
//        RequestPzNCImpl2();
//        RequestPzNCImpl();
    }

    /**
     * 通过工单号 查询工艺系统 获取工艺数据 校对销售预测表
     */
    @Transactional
    public void CheckWord(String WORK) throws Exception {
        String retmessage = "";
        String wsdl = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\" xmlns:sie=\"http://schemas.datacontract.org/2004/07/SIE.PMS.Service.SAP.QualityTrackBills.InParams\" xmlns:sie1=\"http://schemas.datacontract.org/2004/07/SIE.PMS.Service\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <tem:QualityTrackBill>\n" +
                "         <!--Optional:-->\n" +
                "         <tem:QTB_IN_PARAMS>\n" +
                "          \n" +
                "            <sie:qtbInParam>\n" +
                "               <!--Zero or more repetitions:-->\n" +
                "               <sie:QTB_IN_PARAM>\n" +
                "                  <!--Optional:-->\n" +
                "                  <sie:WORK_ORDER_NO>" + WORK + "</sie:WORK_ORDER_NO>\n" +
                "               </sie:QTB_IN_PARAM>\n" +
                "            </sie:qtbInParam>\n" +
                "         </tem:QTB_IN_PARAMS>\n" +
                "      </tem:QualityTrackBill>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>\n";

        String urlString = "http://10.10.1.196:8733/QrCodeService?wsdl";
        HttpURLConnection httpConn = null;
        OutputStream out = null;
        String returnXml = "";
        try {
            httpConn = (HttpURLConnection) new URL(urlString).openConnection();
            httpConn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            httpConn.setRequestProperty("SOAPAction", "http://tempuri.org/IQrCodeService/QrCodeProcess");

            httpConn.setRequestMethod("POST");
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            httpConn.connect();
            out = httpConn.getOutputStream(); // 获取输出流对象
            httpConn.getOutputStream().write(wsdl.getBytes("UTF-8")); // 将要提交服务器的SOAP请求字符流写入输出流
            out.flush();
            out.close();
            int code = httpConn.getResponseCode(); // 用来获取服务器响应状态
            String tempString = null;
            StringBuffer sb1 = new StringBuffer();
            if (code == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(httpConn.getInputStream(),
                                "UTF-8"));
                while ((tempString = reader.readLine()) != null) {
                    sb1.append(tempString);
                }
                if (null != reader) {
                    reader.close();
                }
            } else {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(httpConn.getErrorStream(),
                                "UTF-8"));
                // 一次读入一行，直到读入null为文件结束
                while ((tempString = reader.readLine()) != null) {
                    sb1.append(tempString);
                }
                if (null != reader) {
                    reader.close();
                }
            }
            // 响应报文
            returnXml = sb1.toString();
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            retmessage = "发送失败";
//            throw new BusinessException(retmessage);
        }
        System.out.println(returnXml.toString() + "");
    }

    public static void RequestPzNCImpl2() throws IOException {
        String badasy = "8353782";
        String SOURCE_SYSTEM_ID = "创智";
        String TARGET_SYSTEM_ID = "sap";


        String wsdl = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\" xmlns:sie=\"http://schemas.datacontract.org/2004/07/SIE.PMS.Service.SAP.QualityTrackBills.InParams\" xmlns:sie1=\"http://schemas.datacontract.org/2004/07/SIE.PMS.Service\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <tem:QualityTrackBill>\n" +
                "         <!--Optional:-->\n" +
                "         <tem:QTB_IN_PARAMS>\n" +
                "           \n" +
                "            <!--Optional:-->\n" +
                "            <sie:qtbInParam>\n" +
                "               <!--Zero or more repetitions:-->\n" +
                "               <sie:QTB_IN_PARAM>\n" +
                "                  <!--Optional:-->\n" +
                "                  <sie:WORK_ORDER_NO>83537924</sie:WORK_ORDER_NO>\n" +
                "               </sie:QTB_IN_PARAM>\n" +
                "            </sie:qtbInParam>\n" +
                "         </tem:QTB_IN_PARAMS>\n" +
                "      </tem:QualityTrackBill>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";
        System.out.println(wsdl);
        String urlString = "http://10.10.1.196:8733/QualityTrackBillService";
        // String content = "以上格式的字符串";
        // HttpClient发送SOAP请求
        int timeout = 10000;
        System.out.println("HttpClient 发送SOAP请求");
        HttpClient client = new HttpClient();
        // 如果需要用户名密码验证；不需要验证登录则不需要以下4行
//        String username = "saprfc";
//        String password = "password00";
//        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
//                username, password);
//        client.getState().setCredentials(AuthScope.ANY, creds);

        PostMethod postMethod = new PostMethod(urlString);
        // 设置连接超时
        client.getHttpConnectionManager().getParams()
                .setConnectionTimeout(timeout);
        // 设置读取时间超时
        client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
        // 然后把Soap请求数据添加到PostMethod中
        RequestEntity requestEntity = new StringRequestEntity(wsdl,
                "text/xml", "UTF-8");

        // 设置请求头部，否则可能会报 “no SOAPAction header” 的错误
        postMethod.setRequestHeader("SOAPAction", "");
        // 设置请求体
        postMethod.setRequestEntity(requestEntity);
        int status = client.executeMethod(postMethod);

        if (status == 200) {// 成功
            InputStream is = postMethod.getResponseBodyAsStream();
            // 获取请求结果字符串
            String result = IOUtils.toString(is);
            System.out.println("返回结果:" + result);
        } else {
            System.out.println("错误代码：" + status + ":"
                    + postMethod.getResponseBodyAsString());
        }

    }

    public static String RequestPzNCImpl() {
        String as = "83537924";
        String wsdl = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\" xmlns:sie=\"http://schemas.datacontract.org/2004/07/SIE.PMS.Service.SAP.QualityTrackBills.InParams\" xmlns:sie1=\"http://schemas.datacontract.org/2004/07/SIE.PMS.Service\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <tem:QualityTrackBill>\n" +
                "         <!--Optional:-->\n" +
                "         <tem:QTB_IN_PARAMS>\n" +
                "            <!--Optional:-->\n" +
                "            <sie:qtbInParam>\n" +
                "               <!--Zero or more repetitions:-->\n" +
                "               <sie:QTB_IN_PARAM>\n" +
                "                  <!--Optional:-->\n" +
                "                  <sie:WORK_ORDER_NO>83537924</sie:WORK_ORDER_NO>\n" +
                "               </sie:QTB_IN_PARAM>\n" +
                "            </sie:qtbInParam>\n" +
                "         </tem:QTB_IN_PARAMS>\n" +
                "      </tem:QualityTrackBill>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";


        String urlString = "http://10.10.1.196:8733/QualityTrackBillService?wsdl";
        HttpURLConnection httpConn = null;
        OutputStream out = null;
        String returnXml = "";
        try {
            httpConn = (HttpURLConnection) new URL(urlString).openConnection();
            httpConn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            httpConn.setRequestProperty("SOAPAction", "http://tempuri.org/IQualityTrackBillService/QualityTrackBill");

            httpConn.setRequestMethod("POST");
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            httpConn.connect();
            out = httpConn.getOutputStream(); // 获取输出流对象
            httpConn.getOutputStream().write(wsdl.getBytes("UTF-8")); // 将要提交服务器的SOAP请求字符流写入输出流
            out.flush();
            out.close();
            int code = httpConn.getResponseCode(); // 用来获取服务器响应状态
            String tempString = null;
            StringBuffer sb1 = new StringBuffer();
            if (code == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(httpConn.getInputStream(),
                                "UTF-8"));
                while ((tempString = reader.readLine()) != null) {
                    sb1.append(tempString);
                }
                if (null != reader) {
                    reader.close();
                }
            } else {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(httpConn.getErrorStream(),
                                "UTF-8"));
                // 一次读入一行，直到读入null为文件结束
                while ((tempString = reader.readLine()) != null) {
                    sb1.append(tempString);
                }
                if (null != reader) {
                    reader.close();
                }
            }
            // 响应报文
            returnXml = sb1.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        System.out.println(value.toString());
        System.out.println(returnXml.toString());
        return returnXml;
    }
}
